<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否VIP" prop="vip">
       <el-select v-model="queryParams.vip" class="flex1" placeholder="请选择是否VIP" style="width: 100%;">
          <el-option v-for="item in vipOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
       </el-select>
      </el-form-item>
      <el-form-item label="是否启用" prop="enabled">
       <el-select v-model="queryParams.enabled" class="flex1" placeholder="请选择是否启用" style="width: 100%;">
          <el-option v-for="item in enabledOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
       </el-select>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
       <el-select v-model="queryParams.sex" class="flex1" placeholder="请选择性别" style="width: 100%;">
          <el-option v-for="item in sexOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
       </el-select>
      </el-form-item>
      <el-form-item label="城市" prop="loginCityName">
        <el-input
          v-model="queryParams.loginCityName"
          placeholder="请输入城市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column label="用户ID" align="center" prop="id" />
      <el-table-column label="环信ID" align="center" prop="emUsername" />
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="手机号码" align="center" prop="phone" />
      <el-table-column label="微信号" align="center" prop="wechat" />
      <el-table-column label="头像地址" align="center" prop="headPic">
        <template slot-scope="scope">
          <el-image style="width: 100%; height: 100px" :src="scope.row.headPic"
            :preview-src-list="[scope.row.headPic]">
          </el-image>
        </template>
      </el-table-column>
    <!--  <el-table-column label="花粉余额" align="center" prop="totalAmount" />
      <el-table-column label="花蜜余额" align="center" prop="incomeAmount" /> -->
      <el-table-column label="是否" align="center" prop="vip" >
          <template slot-scope="scope">
            <div v-if="scope.row.vip == 0">否</div>
            <div v-if="scope.row.vip == 1">是</div>
          </template>
      </el-table-column>

      <el-table-column label="VIP等级" align="center" prop="vipLevel" >
          <template slot-scope="scope">
            <div v-if="scope.row.vipLevel == 1">白银</div>
            <div v-if="scope.row.vipLevel == 2">黄金</div>
            <div v-if="scope.row.vipLevel == 3">钻石</div>
            <div v-if="scope.row.vipLevel == 4">黑金</div>
          </template>
      </el-table-column>


      <!-- <el-table-column label="VIP过期时间" align="center" prop="vipExpireTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.vipExpireTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <div v-if="scope.row.sex == 'M'">男</div>
          <div v-if="scope.row.sex == 'F'">女</div>
        </template>
      </el-table-column>
      <el-table-column label="城市" align="center" prop="loginCityName" />
      <el-table-column label="详细地址" align="center" prop="detailAddress" />
      <el-table-column label="是否精选" align="center" prop="isChoose">
        <template slot-scope="scope">
          <div v-if="scope.row.isChoose == 0">否</div>
          <div v-if="scope.row.isChoose == 1">是</div>
        </template>
      </el-table-column>

      <el-table-column label="用户状态" align="center" prop="enabled" >
        <template slot-scope="scope">
          <div v-if="scope.row.enabled == 1">启用</div>
          <div v-if="scope.row.enabled == 2">禁用</div>
          <div v-if="scope.row.enabled == 3">VIP用户</div>
        </template>
      </el-table-column>
      <el-table-column label="注册时间" align="center" prop="registerTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registerTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="登录时间" align="center" prop="loginTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.loginTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="virtuallyStatus" />
      <!-- <el-table-column label="虚拟距离" align="center" prop="virtuallyDistance" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelUser(scope.row)"
            v-hasPermi="['user:user:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入只能是手机号码登陆" />
        </el-form-item>
        <el-form-item label="头像地址" prop="headPic">
         <el-upload class="avatar-uploader" action="" :http-request="ossUploadFun" :show-file-list="false">
         	<img v-if="form.headPic" :src="form.headPic" style="width: 100%;height: 100px;" class="avatar">
         	<i v-else class="el-icon-plus avatar-uploader-icon" ></i>
         </el-upload>
        </el-form-item>
        <el-form-item label="是否VIP" prop="vip">
          <el-select v-model="form.vip" class="flex1" placeholder="请选择是否VIP" style="width: 100%;">
            <el-option v-for="item in vipOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="VIP等级" prop="vipLevel">
          <el-select v-model="form.vipLevel" class="flex1" placeholder="请选择VIP等级" style="width: 100%;">
            <el-option v-for="item in vipLevelOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
       <el-form-item label="VIP过期时间" prop="vipExpireTime">
         <el-date-picker clearable
           v-model="form.vipExpireTime"
           type="date"
           value-format="yyyy-MM-dd"
           placeholder="请选择VIP过期时间">
         </el-date-picker>
       </el-form-item>
       <el-form-item label="是否精选" prop="isChoose">
           <el-select v-model="form.isChoose" class="flex1" placeholder="请选择是否精选" style="width: 100%;">
             <el-option v-for="item in vipOptions" :key="item.value" :label="item.label" :value="item.value">
             </el-option>
           </el-select>
       </el-form-item>

      <el-form-item label="是否启用" prop="enabled">
          <el-select v-model="form.enabled" class="flex1" placeholder="请选择是否启用" style="width: 100%;">
            <el-option v-for="item in enabledOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
      </el-form-item>
      <!-- <el-form-item label="虚拟距离/km" prop="virtuallyDistance">
        <el-input v-model="form.virtuallyDistance" placeholder="请输入虚拟距离" />
      </el-form-item> -->
     <el-form-item label="状态" prop="virtuallyStatus">
       <el-input v-model="form.virtuallyStatus" placeholder="请输入状态" />
     </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 修改用户余额 -->
    <el-dialog :title="updTitle" :visible.sync="updOpen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="updRules" label-width="80px">
       <el-form-item label="类型" prop="vip">
         <el-select v-model="form.vip" class="flex1" placeholder="请选择类型" style="width: 100%;">
           <el-option v-for="item in updTypeOptions" :key="item.value" :label="item.label" :value="item.value">
           </el-option>
         </el-select>
       </el-form-item>
       <el-form-item label="运算符" prop="enabled">
           <el-select v-model="form.enabled" class="flex1" placeholder="请选择运算符" style="width: 100%;">
             <el-option v-for="item in operationOptions" :key="item.value" :label="item.label" :value="item.value">
             </el-option>
           </el-select>
         </el-form-item>
        <el-form-item label="金额" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入余额" maxlength="5"/>
        </el-form-item>
        <el-form-item label="改动备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入余额改动备注" />
            <span style="color: red;font-size: 12px;">余额,收益改动备注,填写将推送信息给用户</span>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormUpd">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 发送消息 -->
    <el-dialog :title="senditle" :visible.sync="sendOpen" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="sendRules" label-width="80px">

        <el-form-item label="选择用户" prop="userId">
        	<el-select v-model="form.userId" @change="optionChange" class="flex1" placeholder="请选择用户" style="width: 100%;">
        	    <el-option
        	      v-for="item in sendUserList"
        	      :key="item.userId"
        	      :label="item.nickName"
        	      :value="item.userId">
        	    </el-option>
        	  </el-select>
        </el-form-item>
        <el-form-item label="消息内容" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入发送内容" />
            <!-- <span style="color: red;font-size: 12px;">余额,收益改动备注,填写将推送信息给用户</span> -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormSendMsg">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser, updateUserAmount,qrySaveData,sendUserMsg,joinMsgSend,updAddress, joinManage,handleDelUser } from "@/api/user/user";

export default {
  name: "User",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 修改用户余额弹出层
      updTitle: "",
      updOpen: false,
      sendOpen: false,
      sendTitle: "",
      // 查询参数
      queryParams: {
        manageType:1,
        pageNum: 1,
        pageSize: 10,
        nickName: null,
        emUsername: null,
        phone: null,
        enabled: null,
        headPic: null,
        baseInfoComplete: null,
        vip: null,
        vipLevel: null,
        age: null,
        sex: null,
        isChoose: null,
        idCard: null,
        name: null,
        avatarAttachmentId: null,
        birthday: null,
        constellation: null,
        height: null,
        bodyWeight: null,
        registerTime: null,
        realPersonAuth: null,
        loginTime: null,
        loginStatus: null,
        loginPointX: null,
        loginPointY: null,
        loginCityCode: null,
        loginCityName: null,
        loginOutTime: null,
        clientVersion: null,
        userType: null,
        macAddress: null,
        deviceName: null,
        logoutType: null,
        logoutContent: null,
        isOfficial: null,
        voice: null,
        video: null,
        education: null,
        secrecy: null,
        topCard: null,
        giftCount: null,
        amount: null,
        topTime: null,
        topEnd: null,
        hideView: null,
        serverTime: null,
        interest: null,
        notice: null,
        orderMsg: null,
        shortMsg: null,
        alipayUserId: null,
        isPlan: null,
        planTime: null,
        userTag: null,
        onlineNotice: null,
        isStealth: null,
        isGift: null,
        lable: null,
        professional: null,
        chatCount: null,
        inviteCount: null,
        unlockWechat: null,
        goWay: null,
        exposureTime: null,
        updatePhone: null,
        isDisplay: null,
        backBirthday: null,
        inteCode: null,
        inteName: null,
        virtuallyDistance:0,
        virtuallyStatus: null
      },
      // 表单参数
      form: {},
      vipLevelOptions: [{
      	value: 1,
      	label: '白银'
      }, {
      	value: 2,
      	label: '黄金'
      }, {
      	value: 3,
      	label: '钻石'
      }, {
      	value: 4,
      	label: '黑金'
      }],
      vipOptions: [{
      	value: 0,
      	label: '否'
      }, {
      	value: 1,
      	label: '是'
      }],
      userTypeOptions: [{
      	value: 0,
      	label: '普通用户'
      }, {
      	value: 1,
      	label: '咨询师'
      }],
       sexOptions: [{
        value: '',
        label: '全部'
       }, {
        value: 'F',
        label: '女'
       }, {
        value: 'M',
        label: '男'
       }],
      enabledOptions: [{
      	value: 1,
      	label: '启用'
      }, {
      	value: 2,
      	label: '禁用'
      }, {
      	value: 3,
      	label: 'VIP用户'
      }],
      updTypeOptions: [{
      	value: 1,
      	label: '花粉'
      }, {
      	value: 2,
      	label: '花蜜'
      }],
      operationOptions: [{
      	value: 1,
      	label: '增加'
      }, {
      	value: 2,
      	label: '减少'
      }],
      // 表单校验
      rules: {
        nickName: [
          { required: true, message: "昵称不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "手机号码不能为空", trigger: "blur" }
        ],
        headPic: [
          { required: true, message: "头像不能不能为空", trigger: "change" }
        ],
      },
      updRules: {
        vip: [
          { required: true, message: "类型不能为空", trigger: "blur" }
        ],
        enabled: [
          { required: true, message: "运算符不能为空", trigger: "blur" }
        ],
        totalAmount: [
          { required: true, message: "金额不能为空", trigger: "change" }
        ],
      },
      sendRules: {
        userId: [
          { required: true, message: "请选择用户", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "请输入消息内容", trigger: "blur" }
        ],
      },
    };
  },
  sendUserList:[],//发送方用户列表
  created() {
    this.getList();
    this.qrySaveDataFun();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 初始化用户信息
    qrySaveDataFun() {
    	qrySaveData().then(res => {
    		if(res.code == 200) {
    			this.sendUserList = res.data.sendUserList;
    		}
    	})
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.updOpen = false;
      this.sendOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        nickName: null,
        totalAmount: null,
        incomeAmount: null,
        remark: null,
        phone: null,
        enabled: null,
        headPic: null,
        baseInfoComplete: null,
        vip: null,
        vipLevel: null,
        age: null,
        sex: null,
        isChoose: null,
        idCard: null,
        name: null,
        avatarAttachmentId: null,
        birthday: null,
        constellation: null,
        height: null,
        bodyWeight: null,
        registerTime: null,
        updateTime: null,
        realPersonAuth: null,
        loginTime: null,
        loginStatus: 0,
        loginPointX: null,
        loginPointY: null,
        loginCityCode: null,
        loginCityName: null,
        loginOutTime: null,
        clientVersion: null,
        userType: null,
        macAddress: null,
        deviceName: null,
        logoutType: null,
        logoutContent: null,
        isOfficial: null,
        voice: null,
        video: null,
        education: null,
        secrecy: null,
        topCard: null,
        giftCount: null,
        amount: null,
        topTime: null,
        topEnd: null,
        hideView: null,
        serverTime: null,
        interest: null,
        notice: null,
        orderMsg: null,
        shortMsg: null,
        alipayUserId: null,
        isPlan: null,
        planTime: null,
        userTag: null,
        onlineNotice: null,
        isStealth: null,
        isGift: null,
        lable: null,
        professional: null,
        chatCount: null,
        inviteCount: null,
        unlockWechat: null,
        goWay: null,
        exposureTime: null,
        updatePhone: null,
        isDisplay: null,
        backBirthday: null,
        inteCode: null,
        inteName: null,
        price: null,
        virtuallyDistance:0,
        virtuallyStatus: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户";
      });
    },

    handleUpdateAmount(row){
      this.reset();
      this.form.id = row.id
      this.updOpen = true;
      this.updTitle = "修改余额";
    },
    handleSendMsg(row){
      this.reset();
      this.form.id = row.id
      this.sendOpen = true;
      this.sendTitle = "发送消息";
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    submitFormUpd() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUserAmount(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.updOpen = false;
              this.getList();
            });
          } else {
              this.$modal.msgError("修改失败");
          }
        }
      });
    },
    // 发送消息
    submitFormSendMsg() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            sendUserMsg(this.form).then(response => {
              this.$modal.msgSuccess("发送成功");
              this.sendOpen = false;
              this.getList();
            });
          } else {
              this.$modal.msgError("发送失败");
          }
        }
      });
    },
    // 图片上传
    ossUploadFun(file) {
    	this.ossUpload(file.file).then(res => {
    		this.form.headPic = res
    	})
    },
    /** 加入消息推送 */
    handleJoinMsg(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否将该用户加入消息推送列表？').then(function() {
        return joinMsgSend(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("加入成功");
      }).catch(() => {});
    },
    // 删除用户
  handleDelUser(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否将该用户移除？').then(function() {
        return handleDelUser(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("移除成功");
      }).catch(() => {});
    },

    // 加入旗下
    handleJoinManage(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否将该用户加入当前登录账号旗下？').then(function() {
        return joinManage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("加入成功");
      }).catch(() => {});
    },

    // 更新详细地址
    handleUpdAddress(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否更新该用户的详细地址？如确认更新可能有3秒延迟，请刷新列表').then(function() {
        return updAddress(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("更新成功,请等待");
      }).catch(() => {});
    },


    /** 导出按钮操作 */
    handleExport() {
      this.download('user/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
